var pnum = "";
var initData = function(num){
       var param = {
           page_num:num,
           page_size:"10"
       }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findDesignTemplate",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	var pageCount = data.pager.pageCount;
	        	var dataObj ={
	        			"list":data.list,
	         			"dataTool":{
	         				"title":{
	          			      	"template_name": "合成模板名称"
	          			      	,"template_code": "合成模板编号"
	          			      	,"version_number":"版本号"
	          			      	,"project_design_phase_name":"设计阶段名称"
	          			      	,"project_design_major_name":"设计专业名称"
	          			      	,"project_design_content_name":"设计内容名称"
	          			      	,"create_time":"创建时间"
	          			      	,"update_time":"修改时间"
	          			      	,"operation":"操作"
	            			},
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"},
							       {"event":"editCompoundRule","name":"编辑合成规则"},
							       {"event":"delObj","name":"删除"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
	        },
	        error: function () {
	        }
	    });
   }
   
   //编辑合成规则
   var editCompoundRule = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"id",
						    "mention_data":"选择提入资料信息",
						    "design_cell_data":"选择设计单元信息",
						    "compound_template":"选择模板信息",
						    "compound_rule":"模板合成规则"
					},"text":{
						    "id":obj.id
						    ,"mention_data":""
						    ,"design_cell_data":""
							,"compound_template":""
							,"compound_rule":obj.compound_rule==null?"":obj.compound_rule
					},"input":{
						    "id":"hidden"
						    ,"mention_data":"checkbox"
						    ,"design_cell_data":"new1_checkbox"
							,"compound_template":"new_checkbox"
							,"compound_rule":"textarea"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopenCompound(contentDiv());
		init_mention_data(obj.id,obj.design_content_id,obj.design_phase_id);
		init_design_cell_data(obj.id,obj.design_content_id,obj.design_phase_id);
		init_compound_template(obj.id,obj.design_content_id,obj.design_phase_id);
   }
   
   //渲染弹窗
   var layopenCompound = function(content){
       layer.open({
           title:'配置模板合成规则'
           ,content: content
           ,area: ['800px', '600px']
       	   ,scrollbar: false
           ,btn: ['确认', '取消']
           ,yes: function(index,layero){ //确认
               var data = $(layero).find("form").serializeJSON();
               if(data.compound_rule == ""){
       			var obj = $("textarea[name='compound_rule']");
    			obj.focus();
    			layer.tips('请填写此字段!', obj);
    			return false;
               }
               var mention_data = new Array();
               var design_cell_data = new Array();
               var compound_template = new Array();
			   $.each($("#checkbox").children("input[type=checkbox]:checked"),function(){
				   mention_data.push($(this).attr("name"));
			   });
			   $.each($("#new1_checkbox").children("input[type=checkbox]:checked"),function(){
				   design_cell_data.push($(this).attr("name"));
			   });
			   $.each($("#new_checkbox").children("input[type=checkbox]:checked"),function(){
				   compound_template.push($(this).attr("name"));
			   });
/*			   alert(mention_data.join(","));
			   alert(design_cell_data.join(","));
			   alert(compound_template.join(","));*/
				$.post(base+"/dataCenter/businessdata/updateCompoundRule",{
					id:data.id==null?"":data.id,
					mention_data:$.trim(mention_data.join(",")),
					design_cell_data:$.trim(design_cell_data.join(",")),
					compound_template:$.trim(compound_template.join(",")),
					compound_rule:$.trim(data.compound_rule)
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
           }
       });
   }
   
   var init_compound_template = function(template_id , content_id , phase_id){
		  if(template_id == "" || content_id == "" || phase_id == ""){
			  return false;
		  }
		  $.post(base+"/dataCenter/businessdata/findCompoundTemplateByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
		   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var checkboxString = "";
	   	                 $.each(data, function(i,item){
   	                		if(item.is_include != null && item.is_include != ""){
   	                			checkboxString += " <input name = "+item.id+" title = "+item.template_name+" type='checkbox' checked='' >";
   	                		}else{
   	                			checkboxString += " <input name = "+item.id+" title = "+item.template_name+" type='checkbox'>";
   	                		}
	   	                });
		   	                $("#new_checkbox").html(checkboxString);
		   	      		}else{
		   	      		  	$("#new_checkbox").html("");
		   	      		}
		     		    form.render(); 
		  });
	   
   }
   
   
   //初始化该设计阶段下该设计内容下的提入资料
   var init_mention_data = function(template_id , content_id , phase_id){
		  if(template_id == "" || content_id == "" || phase_id == ""){
			  return false;
		  }
		  $.post(base+"/dataCenter/businessdata/findMentionDataByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
		   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var checkboxString = "";
	   	                 $.each(data, function(i,item){
   	                		if(item.is_include != null && item.is_include != ""){
   	                			checkboxString += " <input name = "+item.id+" title = "+item.mention_information_name+" type='checkbox' checked='' >";
   	                		}else{
   	                			checkboxString += " <input name = "+item.id+" title = "+item.mention_information_name+" type='checkbox'>";
   	                		}
	   	                });
		   	                $("#checkbox").html(checkboxString);
		   	      		}else{
		   	      		  	$("#checkbox").html("");
		   	      		}
		     		    form.render(); 
		  });
	   
   }
   
   //初始化该设计阶段下该设计内容下的设计常量
   var init_constant_data = function(template_id , content_id , phase_id){
		  if(template_id == "" || content_id == "" || phase_id == ""){
			  return false;
		  }
		  $.post(base+"/dataCenter/businessdata/findConstantDataByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
		   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var checkboxString = "";
	   	                 $.each(data, function(i,item){
   	                		if(item.is_include != null && item.is_include != ""){
   	                			checkboxString += " <input name = "+item.id+" title = "+item.constant_table_name+" type='checkbox' checked='' >";
   	                		}else{
   	                			checkboxString += " <input name = "+item.id+" title = "+item.constant_table_name+" type='checkbox'>";
   	                		}
	   	                });
		   	                $("#new_checkbox").html(checkboxString);
		   	      		}else{
		   	      		  	$("#new_checkbox").html("");
		   	      		}
		     		    form.render(); 
		  });
   }
   
   //初始化该设计阶段下该设计内容下的设计单元信息
   var init_design_cell_data = function(template_id , content_id , phase_id){
		  if(template_id == "" || content_id == "" || phase_id == ""){
			  return false;
		  }
		  $.post(base+"/dataCenter/businessdata/findDesignCellDataByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
		   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var checkboxString = "";
	   	                 $.each(data, function(i,item){
   	                		if(item.is_include != null && item.is_include != ""){
   	                			checkboxString += " <input name = "+item.id+" title = "+item.design_cell_table_name+" type='checkbox' checked='' >";
   	                		}else{
   	                			checkboxString += " <input name = "+item.id+" title = "+item.design_cell_table_name+" type='checkbox'>";
   	                		}
	   	                });
		   	                $("#new1_checkbox").html(checkboxString);
		   	      		}else{
		   	      		  	$("#new1_checkbox").html("");
		   	      		}
		     		    form.render(); 
		  });
   }
   
   //删除设计模板
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delDesignTemplateById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   var updateObj = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"id",
						    "template_name":"模板名称",
					      	"template_code": "模板编号",
					      	"project_design_phase_name":"所属设计阶段",
					      	"project_design_major_name":"所属专业",
					      	"project_design_content_name":"所属设计内容"
					},"text":{
						    "id":obj.id
						    ,"template_name":obj.template_name
						    ,"template_code":obj.template_code
							,"project_design_phase_name":obj.project_design_phase_name
							,"project_design_major_name":obj.project_design_major_name
							,"project_design_content_name":obj.project_design_content_name
					},"input":{
						    "id":"hidden"
						    ,"template_name":"text"
						    ,"template_code":"text"
							,"project_design_phase_name":"text_readonly"
							,"project_design_major_name":"text_readonly"
							,"project_design_content_name":"text_readonly"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
   }
   
   //添加合成模板资料
   var addData = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "template_name":"模板名称",
					      	"template_code": "模板编号",
					      	"design_phase_id":"所属设计阶段",
					      	"design_major_id":"所属专业",
					      	"design_content_id":"所属设计内容"
					},"text":{
						    "template_name":""
						    ,"template_code":""
							,"design_phase_id":""
							,"design_major_id":""	
							,"design_content_id":""
					},"input":{
						    "template_name":"text"
						    ,"template_code":"text"
							,"design_phase_id":"select"
							,"design_major_id":"new_select"
							,"design_content_id":"select"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}

		layopen(contentDiv());
		initPhase();
		initDesignMajor();
		chanageMajorselect();
   }
   
   //渲染弹窗
   var layopen = function(content){
		layer.open({
		  	title:'更新合成模板'
			,content: content
	    	,area: ['800px', '600px']
			,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
  		  	    var data = $(layero).find("form").serializeJSON();
  		  	    if(!verify_data(data)){
  		  	    	return false;
  		  	    }
				$.post(base+"/dataCenter/businessdata/updateTemplate",{
					id:data.id==null?"":data.id,
					template_name:$.trim(data.template_name),
					template_code:$.trim(data.template_code),
					design_phase_id:data.design_phase_id,
					design_major_id:data.design_major_id,
					design_content_id:data.design_content_id
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }
   
   //校验数据
   var verify_data = function(data){
		if(data.template_name == ""){
			var obj = $("input[name='template_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.template_code == ""){
			var obj = $("input[name='template_code']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.design_phase_id == ""){
			alert("请选择设计阶段!");
			return false;
		}
		if(data.design_major_id == ""){
			alert("请选择设计专业");
			return false;
		}
		if(data.design_content_id == ""){
			alert("请选择设计内容!");
			return false
		}
		return true;
   }
   
   var initPhase = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
 	   		  	function(data,status){
     		    	if(data != null && data.list != null){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
   	                });
   	                $("select[name='design_phase_id']").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("select[name='design_phase_id']").html('<option value=""></option>');
   	      		}
     		    form.render(); 
 	  });
    }
   
   var initDesignMajor = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
    		  	function(data,status){
      		    	if(data!=''){
    	      		  	 var optionstring = "";
    	                 $.each(data.list, function(i,item){
    	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
    	                });
    	                $("select[name='design_major_id']").html('<option value=""></option>' + optionstring);
    	      		}else{
    	      		  	$("select[name='design_major_id']").html('<option value=""></option>');
    	      		}
      		    form.render(); 
   });
   }
   
   //监听专业下拉选
   var chanageMajorselect = function(){
   		form.on("select(new_select)",function(value){
   			var major_id = value.value;
   			initContent(major_id);
   		});
   }
   
   var initContent = function(major_id){
 	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
 	   		  	function(data,status){
 	     		    	if(data!=''){
 	   	      		  	 var optionstring = "";
 	   	                 $.each(data.list, function(i,item){
 	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
 	   	                });
 	   	                $("select[name='design_content_id']").html('<option value=""></option>' + optionstring);
 	   	      		}else{
 	   	      		  	$("select[name='design_content_id']").html('<option value=""></option>');
 	   	      		}
 	     		    form.render(); 
 	  });
   }
   
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addData);
   });